
import json
import pickle
import sqlite3
import sys
import urllib
import urllib2
import xml.dom.minidom

'''
response = getPage('http://www.bharatmovies.com/telugu/watch/telugu-new-release-movies.htm','http://srikanth_nukala:Winter12@corp-55w-proxy.mhc:8080')

sys.stdout.write(response)

sourceXml = open("C:\sourceXml",'w')

sourceXml.write(response)
'''
'''
sourceXml = open("C:\\sourceXml",'r')

newfile = open("C:\\final",'w')

for line in sourceXml:
    newline = line.rstrip('\n')
    newfile.write(newline)

sourceXml.close()
newfile.close()
'''
sourceNew = open("C:\\final",'r')
dataFile = open("C:\\dataFile","w")

doc = xml.dom.minidom.parse(sourceNew)

conn = sqlite3.connect('c:\movdb')

c = conn.cursor();

movMap = {}
     
for node in doc.getElementsByTagName("div"):
    divid = str(node.getAttribute("id"))  
#    print divid.__class__    
    if (divid == 'L1') | (divid == 'L2'):
        if node.hasChildNodes:
            childNode = node.getElementsByTagName("a")[0];
            #fetch mov name year and actor list
            movieStr = str(childNode.firstChild.data)
            quality = ''
            
            for qtag in childNode.getElementsByTagName("i"):
                quality = qtag.firstChild.data
                
            year = movieStr.split("-")[0].strip()
            movName = movieStr.split("-")[1].strip()    
            # fetch the link        
            hrefVal = str(childNode.getAttribute("href"))            
            #sys.stdout.write('%s and year %s and movname %s and quality %s and href %s' % (str(node.childNodes),year,movName,quality,hrefVal))
            innerMap = {}
            innerMap['year'] = str(year)
            innerMap['movName'] = str(movName)
            innerMap['quality'] = str(quality)
            innerMap['hrefVal'] = str(hrefVal)
            movMap[year+movName] = innerMap
            try:   
                #movies (year real, title text, quality text, href text, actors text)       
                c.execute("insert into movies values ('%s','%s','%s','%s','%s','%s')" % (year+movName,year,movName,quality,hrefVal,''))
                conn.commit()
            except sqlite3.IntegrityError:
                print ('duplicate movie name: %s', (year+movName))
        else:
            sys.stdout.write('no child nodes')


c.execute('select * from movies')

for row in c:
    print row
        
c.close()        

'''
L = node.getElementsByTagName("title")
  for node2 in L:
    title = ""
    for node3 in node2.childNodes:
      if node3.nodeType == Node.TEXT_NODE:
        title += node3.data
    mapping[isbn] = title
'''



